import { TEMP, edgeFlexToTempBoxes, pushBoxData } from './box'
import type { MagizTypes, StyleParsed, Temp } from './_imports'

export { handleHorizontal }

function handleHorizontal(
  result: MagizTypes.DataJSON.ModelData,
  parsedStyle: Pick<StyleParsed.FloorResult, 'diverse' | 'horizontal' | 'elevations'>,
  rayLoops: Temp.Ray[][]
) {
  const { diverse, horizontal, elevations } = parsedStyle
  if (diverse) {
    elevations.forEach((elevation) => {
      rayLoops.forEach((rayLoop) => {
        rayLoop.forEach((ray) => {
          horizontal.forEach((params) => {
            const tempBoxes = edgeFlexToTempBoxes(params, ray.direction.length())
            tempBoxes.forEach((tempBoxData) => {
              const matrix = tempBoxData.matrix
                .clone()
                .premultiply(TEMP.makeRotationZ(ray.direction.angle()))
                .premultiply(TEMP.makeTranslation(ray.start.x, ray.start.y, elevation))
              pushBoxData(result, tempBoxData.color, matrix)
            })
          })
        })
      })
    })
  } else {
    rayLoops.forEach((rayLoop) => {
      rayLoop.forEach((ray) => {
        horizontal.forEach((params) => {
          const tempBoxes = edgeFlexToTempBoxes(params, ray.direction.length())
          elevations.forEach((elevation) => {
            tempBoxes.forEach((tempBoxData) => {
              const matrix = tempBoxData.matrix
                .clone()
                .premultiply(TEMP.makeRotationZ(ray.direction.angle()))
                .premultiply(TEMP.makeTranslation(ray.start.x, ray.start.y, elevation))
              pushBoxData(result, tempBoxData.color, matrix)
            })
          })
        })
      })
    })
  }
}
